package com.jiantong.lawEnforcing.impl.manager;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Maps;
import com.jiantong.lawEnforcing.dto.WritDataDto;
import com.jiantong.lawEnforcing.impl.dao.AskAnswerDao;
import com.jiantong.lawEnforcing.impl.dao.DatasDao;
import com.jiantong.lawEnforcing.impl.dao.GoodsDao;
import com.jiantong.lawEnforcing.impl.dao.OriginFieldDao;
import com.jiantong.lawEnforcing.model.AskAnswer;
import com.jiantong.lawEnforcing.model.Datas;
import com.jiantong.lawEnforcing.model.Goods;
import com.jiantong.lawEnforcing.model.OriginField;
import io.terminus.common.utils.JsonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 数据存储相关事务处理
 * Created by mz03 on 2017/9/14.
 */
@Component
public class DatasManager {
    private static final ObjectMapper objectMapper= JsonMapper.nonEmptyMapper().getMapper();
    private final DatasDao datasDao;
    private final OriginFieldDao originFieldDao;
    private final AskAnswerDao askAnswerDao;
    private final GoodsDao goodsDao;
    @Autowired
    public DatasManager(DatasDao datasDao, OriginFieldDao originFieldDao, AskAnswerDao askAnswerDao, GoodsDao goodsDao) {
        this.datasDao = datasDao;
        this.originFieldDao = originFieldDao;
        this.askAnswerDao = askAnswerDao;
        this.goodsDao = goodsDao;
    }

    /**
     * 创建一条信息casesBasicDto
     * @param
     * @return
     */
    @Transactional
    public Integer deletesByOriginIds(WritDataDto writDataDto){

        //删除老数据
        OriginField originField = new OriginField();
        originField.setDetailed_type(writDataDto.getWrit_type());
        List<OriginField> list = originFieldDao.list(originField);
        List<Long> originIds = new ArrayList<>();
        for (OriginField or : list) {
            originIds.add(or.getId());
        }
        Integer count = writDataDto.getCount();
        Map<String,Object> criteria = Maps.newHashMap();
        criteria.put("count",count);
        criteria.put("law_case", writDataDto.getCases_id());
        criteria.put("originIds",originIds);

        Integer integer = datasDao.deletesByOriginIds(criteria); //拿到元数据中指定文书的所有origin_id 根据其删除老数据


        //创建新数据
        Map<String,Object> map =writDataDto.getMap();
        List<Datas> datasList = new ArrayList<>();
        Integer origin_type_two = writDataDto.getWrit_type();
        String writ_two = origin_type_two.toString();
        String type_one = writ_two.substring(0,writ_two.length()-2);
        Integer origin_type_one = new Integer(type_one);
        Object object;
        for (OriginField origin : list) {
            Datas datas = new Datas();
            datas.setLaw_case(writDataDto.getCases_id());
            datas.setOrigin_type_one(origin_type_one);//一级类型
            datas.setOrigin_type_two(origin_type_two);//二级类型
            datas.setCount(count);
            datas.setOrigin_id(origin.getId());
            datas.setOrigin_name(origin.getName());
            object = map.get(origin.getName());
            if (object==null){
                datas.setData(null);
                datasList.add(datas);
            }else {
                datas.setData(object.toString());
                datasList.add(datas);
            }
        }

        Integer integer1 = datasDao.creates(datasList);
        return integer1;
    }

    /**
     *
     * @param
     * @return
     */
    @Transactional
    public Long createAndList(AskAnswer askAnswer, Long lawCase, Integer writType, String idList){
        askAnswerDao.create(askAnswer);
        //查到datas中的
        Datas datas = new Datas();
        datas.setLaw_case(lawCase);
        datas.setOrigin_type_two(writType);
        datas.setOrigin_name(idList);
        List<Datas> datasList = datasDao.list(datas);
        Datas datas1 = datasList.get(0);
        String ids = datas1.getData();
        if (ids == null) {
            List<Long> list = new ArrayList<>();
            list.add(askAnswer.getId());
            try {
                ids = objectMapper.writeValueAsString(list);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            datas1.setData(ids);
            datasDao.update(datas1);
            return askAnswer.getId();
        }else {
            try {
                List<Long> list = objectMapper.readValue(ids, new TypeReference<List<Long>>() {
                });
                list.add(askAnswer.getId());
                ids = objectMapper.writeValueAsString(list);
            } catch (IOException e) {
                e.printStackTrace();
            }
            datas1.setData(ids);
            datasDao.update(datas1);
            return askAnswer.getId();
        }
    }

    /**
     *  创建物品对象并且添加datas中对应的物品idList
     * @param goods
     * @param lawCase
     * @param writType
     * @param idList
     * @return
     */
    @Transactional
    public Long createAndDatas(Goods goods, Long lawCase, Integer writType, String idList){
        goodsDao.create(goods);
        //查到datas中的
        Datas datas = new Datas();
        datas.setLaw_case(lawCase);
        datas.setOrigin_type_two(writType);
        datas.setOrigin_name(idList);
        List<Datas> datasList = datasDao.list(datas);
        Datas datas1 = datasList.get(0);
        String ids = datas1.getData();
        if (ids == null) {
            List<Long> list = new ArrayList<>();
            list.add(goods.getId());
            try {
                ids = objectMapper.writeValueAsString(list);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            datas1.setData(ids);
            datasDao.update(datas1);
            return goods.getId();
        }else {
            try {
                List<Long> list = objectMapper.readValue(ids, new TypeReference<List<Long>>() {
                });
                list.add(goods.getId());
                ids = objectMapper.writeValueAsString(list);
            } catch (IOException e) {
                e.printStackTrace();
            }
            datas1.setData(ids);
            datasDao.update(datas1);
            return goods.getId();
        }
    }



    }
